package com.example.pcbcsmbackend.config;

import com.example.pcbcsmbackend.entity.*;
import com.example.pcbcsmbackend.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class DataInitializer implements CommandLineRunner {
    
    @Autowired
    private AdminService adminService;
    
    @Autowired
    private CategoryService categoryService;
    
    @Autowired
    private ProductService productService;

    @Override
    public void run(String... args) throws Exception {
        initializeData();
    }
    
    private void initializeData() {
        try {
            // 创建默认管理员账号
            createDefaultAdmin();
            
            // 创建商品分类
            createCategories();
            
            System.out.println("数据初始化完成！");
            System.out.println("默认管理员账号 - 用户名: admin, 密码: admin123");
            
        } catch (Exception e) {
            System.err.println("数据初始化失败: " + e.getMessage());
        }
    }
    
    private void createDefaultAdmin() {
        try {
            adminService.createAdmin("admin", "admin123");
            System.out.println("已创建默认管理员账号");
        } catch (Exception e) {
            System.out.println("管理员账号已存在，跳过创建");
        }
    }
    
    private void createCategories() {
        // 创建顶级分类
        Category mcuCategory = categoryService.addCategory("微控制器", null, "各类微控制器芯片");
        Category sensorCategory = categoryService.addCategory("传感器", null, "各类传感器模块");
        Category moduleCategory = categoryService.addCategory("功能模块", null, "各类功能模块");
        Category devBoardCategory = categoryService.addCategory("开发板", null, "各类开发板套件");
        
        // 创建子分类
        categoryService.addCategory("ARM系列", mcuCategory.getCategoryId(), "ARM架构微控制器");
        categoryService.addCategory("AVR系列", mcuCategory.getCategoryId(), "AVR架构微控制器");
        categoryService.addCategory("ESP系列", mcuCategory.getCategoryId(), "乐鑫ESP系列芯片");
        
        categoryService.addCategory("温度传感器", sensorCategory.getCategoryId(), "各类温度检测传感器");
        categoryService.addCategory("压力传感器", sensorCategory.getCategoryId(), "各类压力检测传感器");
        
        System.out.println("已创建商品分类");
    }
    

} 